package com.controller;

import com.pojo.Notice;
import com.pojo.Plot;
import com.pojo.Repairs;
import com.service.*;
import com.vo.LineVo;
import com.vo.Result;
import com.vo.shuidianVo;
import com.vo.welcomtongjiVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static java.util.Comparator.comparing;

@Controller
public class welcome {
    @Resource
    private PlotService plotService;
    @Resource
    private FloormanagerService floormanagerService;
    @Resource
    private HouseService houseService;
    @Resource
    private OwnersService ownersService;
    @Resource
    private StopcarmoneyService stopcarmoneyService;
    @Resource
    private RepairsService repairsService;
    @Resource
    private HydropowerService hydropowerService;
    @Resource
    private PayService payService;
    @Resource
    private RepairQuotationService repairQuotationService;

    @Resource
    private NoticeService noticeService;

    @RequestMapping("/welcome")
    private String hello(HttpServletRequest request) {
        List zilist=new ArrayList();

        List<Plot> plots = plotService.selectPlot();
        for (Plot plot : plots) {
            int selecttotalbyplotid = floormanagerService.selecttotalbyplotid(plot.getId());
            int selecttotalhouse = houseService.selecttotalhouse(plot.getId());
            int totalhouse = ownersService.selecttotalownerByplotid(plot.getId());
            int selecttotalzuanzu = houseService.selecttotalzuanzu(plot.getId());
            welcomtongjiVo welcomtongjiVo=new welcomtongjiVo();
            welcomtongjiVo.setPlot(plot);
            welcomtongjiVo.setTotalfloor(selecttotalbyplotid);
            welcomtongjiVo.setTotalhouse(selecttotalhouse);
            welcomtongjiVo.setTotalpeople(totalhouse);
            welcomtongjiVo.setTotalzupeople(selecttotalzuanzu);
           zilist.add(welcomtongjiVo);
        }
        System.out.println(zilist);
        request.getSession().setAttribute("plot",zilist);
        request.getSession().setAttribute("plots",plots);
        return "welcome";
    }
    @RequestMapping("/line")
    @ResponseBody
    private Result line(){
        List<LineVo> selectline = stopcarmoneyService.selectline();
        return Result.success(selectline);
    }

    @RequestMapping("/zongmoneyline")
    @ResponseBody
    private Result zongmoneyline(){
        List<LineVo> zongmoney=new ArrayList<>();
        List<LineVo> selectline = stopcarmoneyService.selectline();
        List<LineVo> lineVos = hydropowerService.selectallHydropower();
        List<LineVo> selectallpay = payService.selectallpay();
        List<LineVo> selectallrepairquotation = repairQuotationService.selectallrepairquotation();
        List<Integer> zongmonth=new ArrayList<>();
                    for (LineVo vo : lineVos) {
                        if(zongmonth.contains(vo.getMonth())){
                                for (LineVo vos : zongmoney) {
                                    if(vos.getMonth()==vo.getMonth()){
                                        double p=vo.getCount()+vo.getCount();
                                        vos.setCount(p);
                                    }
                                }
                        }else{
                            zongmonth.add(vo.getMonth());
                                LineVo lineVo1=new LineVo();
                                lineVo1.setMonth(vo.getMonth());
                                lineVo1.setCount(vo.getCount());
                                zongmoney.add(lineVo1);
                            }
                    }

            for (LineVo lineVo1 : selectallpay) {
                if(zongmonth.contains(lineVo1.getMonth())) {
                        for (LineVo vo : zongmoney) {
                            if(vo.getMonth()==lineVo1.getMonth()){
                                double p=vo.getCount()+lineVo1.getCount();
                                vo.setCount(p);
                            }
                    }
                }else{
                    zongmonth.add(lineVo1.getMonth());
                    LineVo lineVo2=new LineVo();
                    lineVo2.setMonth(lineVo1.getMonth());
                    lineVo2.setCount(lineVo1.getCount());
                    zongmoney.add(lineVo2);
                }

            }


            for (LineVo lineVo2 : selectallrepairquotation) {
                if(zongmonth.contains(lineVo2.getMonth())) {
                        for (LineVo vo : zongmoney) {
                            if(vo.getMonth()==lineVo2.getMonth()){
                                double p=vo.getCount()+lineVo2.getCount();
                                vo.setCount(p);
                            }
                        }
                }else{
                    zongmonth.add(lineVo2.getMonth());
                    LineVo lineVo3=new LineVo();
                    lineVo3.setMonth(lineVo2.getMonth());
                    lineVo3.setCount(lineVo2.getCount());
                    zongmoney.add(lineVo3);
                }
            }

        System.out.println(zongmoney);
        System.out.println(selectline);
        for (LineVo lineVo : zongmoney) {
            boolean flag=false;
            for (LineVo lineVo1 : selectline) {
                if(lineVo1.getMonth()==lineVo.getMonth()){
                    double p=lineVo1.getCount()+lineVo.getCount();
                    lineVo1.setCount(p);
                    flag=true;
                }
            }
if (flag==false){
    LineVo line=new LineVo();
    line.setMonth(lineVo.getMonth());
    line.setCount(lineVo.getCount());
    selectline.add(line);
}
        }
        selectline.sort(comparing(LineVo::getMonth));
        return Result.success(selectline);
    }

    @RequestMapping("/shuidianline")
    @ResponseBody
    private Result shuidianline(){
        List<shuidianVo> shuidianVos = hydropowerService.selectallByplotidHydropower();
        shuidianVos.sort(comparing(shuidianVo::getMonth));
        return Result.success(shuidianVos);
    }


    @RequestMapping("/selecttoptenrepairs")
    @ResponseBody
    private Result selecttoptenrepairs(){
        List<Repairs> selecttoptenrepairs = repairsService.selecttoptenrepairs();
        return Result.success(selecttoptenrepairs);
    }
    @RequestMapping("/selecttoptennotice")
    @ResponseBody
    private Result selecttoptennotice(){
        List<Notice> selectbytennotice = noticeService.selectbytennotice();
        return Result.success(selectbytennotice);
    }
}
